package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.auto.CollectInfoMapper;
import com.model.auto.CollectInfo;
import com.model.auto.MajorCategoryInfo;
import com.service.ICollectService;
import com.service.IMajorCategoryInfoService;
import com.service.ISchoolInfoV2Service;
import com.util.Page;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhaoyifan
 * @date 2023/9/2 15:43
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectInfoMapper, CollectInfo> implements ICollectService {

    @Autowired
    private ISchoolInfoV2Service schoolService;
    @Autowired
    private IMajorCategoryInfoService majorCategoryInfoService;

    @Override
    public PageInfo<CollectInfo> myList(Page page, Long userId, Integer type, String code) {
        PageInfo<CollectInfo> pageInfo = PageHelper.startPage(page.getPageNum(), page.getPageSize())
                .doSelectPageInfo(() -> {
                    List<CollectInfo> list = this.list(new LambdaQueryWrapper<CollectInfo>()
                            .eq(userId != null && !"".equals(userId), CollectInfo::getUserId, userId)
                            .eq(type != null && !"".equals(type), CollectInfo::getType, type)
                            .eq(code != null && !"".equals(code), CollectInfo::getCode, code)
                    );
                    if(type == 1){
                        //获取收藏院校详情
                        for (CollectInfo collectInfo : list) {
                            if(StringUtils.hasText(collectInfo.getCode())){
                                collectInfo.setSchoolDetail(schoolService.selectByCode(collectInfo.getCode()));
                            }
                            if(Objects.isNull(collectInfo.getSchoolDetail())){
                                collectInfo.setSchoolDetail(schoolService.selectBySchoolName(collectInfo.getName()));
                            }
                        }
                    }
                    if(type == 2){
                        //获取专业父级名称
                        for (CollectInfo collectInfo : list) {
                            if(StringUtils.hasText(collectInfo.getCode())){
                                List<String> codes = new ArrayList<>();
                                codes.add(collectInfo.getCode().substring(0,4));
                                codes.add(collectInfo.getCode().substring(0,2));
                                List<MajorCategoryInfo> categoryInfos = majorCategoryInfoService.list(new LambdaQueryWrapper<MajorCategoryInfo>()
                                        .eq(MajorCategoryInfo::getCatType, 1)
                                        .in(MajorCategoryInfo::getCode, codes)
                                        .orderByAsc(MajorCategoryInfo::getLevel)
                                );
                                List<String> parentName = categoryInfos.stream().map(MajorCategoryInfo::getName).collect(Collectors.toList());
                                collectInfo.setParentName(parentName);
                            }
                        }
                    }
                });
        return pageInfo;
    }

    @Override
    public R add(CollectInfo collectInfo) {
        if(this.isCollect(collectInfo)){
            return R.error("你已收藏该专业");
        }
        collectInfo.setCreateDate(LocalDateTime.now());
        baseMapper.insert(collectInfo);
        return R.ok();
    }


    @Override
    public R cancel(List<CollectInfo> collectInfos, Long userId) {
        for(CollectInfo collectInfo:collectInfos){
            baseMapper.delete(new LambdaQueryWrapper<CollectInfo>()
                    .eq(CollectInfo::getUserId, userId)
                    .eq(CollectInfo::getType, collectInfo.getType())
                    .eq( CollectInfo::getCode, collectInfo.getCode()));
        }
        return R.ok();
    }

    @Override
    public Boolean isCollect(CollectInfo collectInfo) {
        long count = this.count(new LambdaQueryWrapper<CollectInfo>()
                .eq(CollectInfo::getUserId, collectInfo.getUserId())
                .eq(CollectInfo::getType, collectInfo.getType())
                .eq(CollectInfo::getCode, collectInfo.getCode())
                .eq(CollectInfo::getEnabled, 0)
        );
        return count>0;
    }
}
